﻿using System;
using System.Collections.Concurrent;

namespace zijian666.Converts;

public static class ConvertsMapping
{
    private static readonly ConcurrentDictionary<(Type source, Type target), IMappingAction> _mappings = new();


    public static void Map<TSource, TTarget>(Action<TSource, TTarget> action)
        => _mappings[(typeof(TSource), typeof(TTarget))] = new MappingAction<TSource, TTarget>(action);


    internal static void ApplyMap(object source, object target)
    {
        if (source is null || target is null)
        {
            return;
        }

        _mappings.TryGetValue((source.GetType(), target.GetType()), out var mapping);
        mapping?.Map(source, target);
    }

}
